package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        //2.定义队列
        c.queueDeclare(
                "helloworld",
                false,
                false,
                false,
                null);
        //创建回调对象
        DeliverCallback dc = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] body = message.getBody();
                String s = new String(body);
                System.out.println("收到:"+s);
            }
        };
        CancelCallback cc = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };
        //3.消费,接受消息
        c.basicConsume("helloworld",
                true,
                dc,
                cc);
    }
}
